#ifndef __MINIUTIL_FRAME_SHAREFILE_H__
#define __MINIUTIL_FRAME_SHAREFILE_H__

#include <string>
#include <list>
#include "miniutil/thread.h"
#include "miniutil/datatype.h"
#include "miniutil/scope.h"

namespace miniutil { namespace frame {

    typedef miniutil::m_int64_t fs_t;

    class LocalSharedFile
    {
        public:
            LocalSharedFile();
            ~LocalSharedFile();
            int Open(const std::string &flnm);
            int Close();
            int Read(char* buf, fs_t offset, int size);
            int Write(char* buf, fs_t offset, int size);
            int ExistInfo(std::list<std::pair<fs_t, fs_t> >&);
            int Remove();
            int SetLength(fs_t len);
            fs_t GetLength();
            bool IsComplete();
        protected:

            miniutil::mutex mtx;
            FILE* mpdatafile;
            FILE* mpmetafile;
            FILE* mplockfile;
            std::string mfilepath; //abs. path
            bool mbopenpart;
            std::string mmetapath;
            
            //std::list<std::pair<fs_t, fs_t> > mexitinfo;
            miniutil::util::Scope<fs_t> mexitinfo;
            fs_t mlength;
            unsigned int updatesesid;

            int Lock();
            int UnLock();
            int FlushMetaInfo();
            int ReloadMetaInfo();
            FILE* TryOpenOneFile(std::string, std::string option="");

    };

} }

#endif
